package com.singularity.common.controller;

import cn.dev33.satoken.util.SaResult;

import com.singularity.common.domain.dto.role.RoleListDTO;
import com.singularity.common.domain.param.role.RoleAddParam;
import com.singularity.common.domain.param.role.RoleDistributeParam;
import com.singularity.common.domain.param.role.RoleListParam;
import com.singularity.common.domain.param.role.RoleUpdateParam;
import com.singularity.common.service.RoleService;
import com.singularity.utils.page.ResponseDTOWithPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author 天
 *
 * @description: 角色
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色相关")
public class RoleController {

    private final RoleService roleService;

    @Autowired
    public RoleController(RoleService roleService) {
        this.roleService = roleService;
    }

    @ApiOperation("获取角色列表信息")
    @RequestMapping(method = RequestMethod.POST,value = "/list")
    public SaResult getRoleList(@RequestBody RoleListParam roleListParam){
        ResponseDTOWithPage<RoleListDTO> list = roleService.getRoleList(roleListParam);
        return new SaResult(SaResult.CODE_SUCCESS,"查询成功",list);
    }

    @ApiOperation("修改角色信息")
    @RequestMapping(method = RequestMethod.POST,value = "/update")
    public SaResult updateRole(@RequestBody RoleUpdateParam roleUpdateParam){
        int result = roleService.updateById(roleUpdateParam);
        if (result > 0){
            return SaResult.ok("修改成功");
        }else {
            return SaResult.error("修改失败");
        }

    }

    @ApiOperation("新增角色")
    @RequestMapping(method = RequestMethod.POST,value = "/add")
    public SaResult insertRole(@RequestBody RoleAddParam roleAddParam){
        boolean result = roleService.insertRole(roleAddParam);
        if (result){
            return SaResult.ok("添加成功");
        }else {
            return SaResult.error("添加失败");
        }
    }

    @ApiOperation("分配角色权限")
    @RequestMapping(method = RequestMethod.POST,value = "/distribute")
    public SaResult distribute(@RequestBody RoleDistributeParam roleDistributeParam){
        boolean result = roleService.distribute(roleDistributeParam);
        if (result){
            return SaResult.ok("分配成功");
        }else {
            return SaResult.error("分配失败");
        }
    }


}
